
#include "stdafx.h"

#include "PropertiesWnd.h"
#include "Resource.h"
#include "MainFrm.h"
#include "OpenESSO.h"
#include "OpenESSODoc.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

/////////////////////////////////////////////////////////////////////////////
// CResourceViewBar

CPropertiesWnd::CPropertiesWnd()
{
	//application
	m_application= new CApplication();
	pApplicationGroup=new CMFCPropertyGridProperty(_T("Application"));
	pStateGroup= new CMFCPropertyGridProperty(_T("State"));
	pTechnicalGroup= new CMFCPropertyGridProperty(_T("Technical"));

	pAppliName=new CMFCPropertyGridProperty(_T("Name"), (_variant_t) m_application->GetName(), _T("Name of application"));
	pAppliVersion=new CMFCPropertyGridProperty(_T("Version"), (_variant_t) m_application->GetVersion(), _T("Version of application"));
	pAppliDescription=new CMFCPropertyGridProperty(_T("Description"), (_variant_t) m_application->GetDescription(), _T("Brief description of this application"));
	pAppliComment=new CMFCPropertyGridProperty(_T("Comment"), (_variant_t) m_application->GetComment() , _T("Comment about this application"));
	
	pApplicationGroup->AddSubItem(pAppliName);
	pApplicationGroup->AddSubItem(pAppliVersion);
	pApplicationGroup->AddSubItem(pAppliDescription);
	pApplicationGroup->AddSubItem(pAppliComment);

	pAppliEnabled=new CMFCPropertyGridProperty(_T("Enabled"), (_variant_t) true , _T("Specifies the state of the application"));
	pStateGroup->AddSubItem(pAppliEnabled);
	pAppliId=new CMFCPropertyGridProperty(_T("Id"), (_variant_t) m_application->GetId() , _T("Specifies the Id of the application"));
	pTechnicalGroup->AddSubItem(pAppliId);
	//account
	m_account=new CAccount();
	pAccountGroup=new CMFCPropertyGridProperty(_T("Account"));	
	pAccountOldPasswordGroup=new CMFCPropertyGridProperty(_T("Old Password"));

	pAccountLogin=new CMFCPropertyGridProperty(_T("Login"),(_variant_t) m_account->GetLogin(),_T("User Login"));
	pAccountPassword=new CMFCPropertyGridProperty(_T("Password"),(_variant_t) m_account->GetPassword(),_T("User Password"));
	pAccountThirdField=new CMFCPropertyGridProperty(_T("Third Field"),(_variant_t) m_account->GetThirdField(),_T("Third Field"));
	pAccountFourthField=new CMFCPropertyGridProperty(_T("Fourth Field"),(_variant_t) m_account->GetThirdField(),_T("Fourth Field"));
	pAccountGroup->AddSubItem(pAccountLogin);
	pAccountGroup->AddSubItem(pAccountPassword);
	pAccountGroup->AddSubItem(pAccountThirdField);
	pAccountGroup->AddSubItem(pAccountFourthField);
	pAccountOldPassword1=new CMFCPropertyGridProperty(_T("Old Password 1"),(_variant_t) m_account->GetOldPassword1(),_T("User old password 1"));
	pAccountOldPassword2=new CMFCPropertyGridProperty(_T("Old Password 2"),(_variant_t) m_account->GetOldPassword2(),_T("User old password 2"));
	pAccountOldPassword3=new CMFCPropertyGridProperty(_T("Old Password 3"),(_variant_t) m_account->GetOldPassword3(),_T("User old password 3"));
	pAccountOldPassword4=new CMFCPropertyGridProperty(_T("Old Password 4"),(_variant_t) m_account->GetOldPassword4(),_T("User old password 4"));
	pAccountOldPassword5=new CMFCPropertyGridProperty(_T("Old Password 5"),(_variant_t) m_account->GetOldPassword5(),_T("User old password 5"));
	pAccountOldPassword6=new CMFCPropertyGridProperty(_T("Old Password 6"),(_variant_t) m_account->GetOldPassword6(),_T("User old password 6"));
	pAccountOldPasswordGroup->AddSubItem(pAccountOldPassword1);
	pAccountOldPasswordGroup->AddSubItem(pAccountOldPassword2);
	pAccountOldPasswordGroup->AddSubItem(pAccountOldPassword3);
	pAccountOldPasswordGroup->AddSubItem(pAccountOldPassword4);
	pAccountOldPasswordGroup->AddSubItem(pAccountOldPassword5);
	pAccountOldPasswordGroup->AddSubItem(pAccountOldPassword6);
	pAccountEnabled=new CMFCPropertyGridProperty(_T("Enabled"), (_variant_t) true , _T("Specifies the state of this account"));
	pStateGroup->AddSubItem(pAccountEnabled);
	pAccountId=new CMFCPropertyGridProperty(_T("Id"), (_variant_t) m_account->GetId() , _T("Specifies the Id of the account"));
	pTechnicalGroup->AddSubItem(pAccountId);
}

CPropertiesWnd::~CPropertiesWnd()
{
	delete m_application;
}

BEGIN_MESSAGE_MAP(CPropertiesWnd, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_COMMAND(ID_EXPAND_ALL, OnExpandAllProperties)
	ON_UPDATE_COMMAND_UI(ID_EXPAND_ALL, OnUpdateExpandAllProperties)
	ON_WM_SETFOCUS()
	ON_WM_SETTINGCHANGE()
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceViewBar message handlers

void CPropertiesWnd::AdjustLayout()
{
	if (GetSafeHwnd() == NULL)
	{
		return;
	}
	CRect rectClient,rectCombo;
	GetClientRect(rectClient);
	m_wndPropList.SetWindowPos(NULL, rectClient.left, rectClient.top /*+ cyCmb*/ /*+ cyTlb*/, rectClient.Width(), rectClient.Height() /*-(cyCmb/*+cyTlb)*/, SWP_NOACTIVATE | SWP_NOZORDER);
}

int CPropertiesWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rectDummy;
	rectDummy.SetRectEmpty();
	// Create List:
	if (!m_wndPropList.Create(WS_VISIBLE | WS_CHILD, rectDummy, this, 2))
	{
		TRACE0("Failed to create Properties Grid \n");
		return -1;      // fail to create
	}
	InitPropList();
	AdjustLayout();
	return 0;
}

void CPropertiesWnd::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);
	AdjustLayout();
}

void CPropertiesWnd::OnExpandAllProperties()
{
	m_wndPropList.ExpandAll();
}

void CPropertiesWnd::OnUpdateExpandAllProperties(CCmdUI* pCmdUI)
{
}
void CPropertiesWnd::InitPropList()
{
	SetPropListFont();
	// TODO : Low : Make propertieList Dynamic and load it from xml

	m_wndPropList.EnableHeaderCtrl(FALSE);
	m_wndPropList.EnableDescriptionArea();
	m_wndPropList.SetVSDotNetLook();
	m_wndPropList.MarkModifiedProperties();
	m_wndPropList.AddProperty(pApplicationGroup);
	m_wndPropList.AddProperty(pAccountGroup);
	m_wndPropList.AddProperty(pStateGroup);
	m_wndPropList.AddProperty(pAccountOldPasswordGroup);
	m_wndPropList.AddProperty(pTechnicalGroup);
	SetAccountPropertyGridVisible(false);
}

void CPropertiesWnd::OnSetFocus(CWnd* pOldWnd)
{
	CDockablePane::OnSetFocus(pOldWnd);
	m_wndPropList.SetFocus();
}

void CPropertiesWnd::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
	CDockablePane::OnSettingChange(uFlags, lpszSection);
	SetPropListFont();
}
void CPropertiesWnd::SetPropListFont()
{
	::DeleteObject(m_fntPropList.Detach());

	LOGFONT lf;
	afxGlobalData.fontRegular.GetLogFont(&lf);

	NONCLIENTMETRICS info;
	info.cbSize = sizeof(info);

	afxGlobalData.GetNonClientMetrics(info);

	lf.lfHeight = info.lfMenuFont.lfHeight;
	lf.lfWeight = info.lfMenuFont.lfWeight;
	lf.lfItalic = info.lfMenuFont.lfItalic;

	m_fntPropList.CreateFontIndirect(&lf);

	m_wndPropList.SetFont(&m_fntPropList);
}


void CPropertiesWnd::SetApplication(const CApplication& nApp)
{
	*m_application=nApp;
	pAppliName->SetValue((_variant_t) m_application->GetName());
	pAppliVersion->SetValue((_variant_t) m_application->GetVersion());
	pAppliDescription->SetValue((_variant_t) m_application->GetDescription());
	pAppliComment->SetValue((_variant_t) m_application->GetComment());
	//pAppliEnabled->SetValue((_variant_t) m_application->GetEnabled());
	pAppliId->SetValue((_variant_t) m_application->GetId());
	SetAccountPropertyGridVisible(false);
	SetApplicationPropertyGridVisible(true);
}
void CPropertiesWnd::SetAccount(const CAccount& nAcc)
{
	*m_account=nAcc;
	pAccountLogin->SetValue((_variant_t) m_account->GetLogin());
	pAccountPassword->SetValue((_variant_t) m_account->GetPassword());
	pAccountThirdField->SetValue((_variant_t) m_account->GetThirdField());
	pAccountFourthField->SetValue((_variant_t) m_account->GetFourthField());
	pAccountId->SetValue((_variant_t) m_account->GetId());
	//pAccountEnabled->SetValue((_variant_t) m_account->GetEnabled());
	pAccountOldPassword1->SetValue((_variant_t) m_account->GetOldPassword1());
	pAccountOldPassword2->SetValue((_variant_t) m_account->GetOldPassword2());
	pAccountOldPassword3->SetValue((_variant_t) m_account->GetOldPassword3());
	pAccountOldPassword4->SetValue((_variant_t) m_account->GetOldPassword4());
	pAccountOldPassword5->SetValue((_variant_t) m_account->GetOldPassword5());
	pAccountOldPassword6->SetValue((_variant_t) m_account->GetOldPassword6());
	SetApplicationPropertyGridVisible(false);
	SetAccountPropertyGridVisible(true);
}

void CPropertiesWnd::SetApplicationPropertyGridVisible(BOOL bvisible)
{
	if(bvisible)
	{
		pApplicationGroup->Show(true,true);
		pAppliEnabled->Show(true,true);
		pAppliId->Show(true,true);
	}
	else
	{
		pApplicationGroup->Show(false,false);
		pAppliEnabled->Show(false,false);
		pAppliId->Show(false,false);
	}
}
void CPropertiesWnd::SetAccountPropertyGridVisible(BOOL bvisible)
{
	if(bvisible)
	{
		pAccountGroup->Show(true,true);
		pAccountOldPasswordGroup->Show(true,true);
		pAccountEnabled->Show(true,true);
		pAccountId->Show(true,true);
	}
	else
	{
		pAccountGroup->Show(false,false);
		pAccountOldPasswordGroup->Show(false,false);
		pAccountEnabled->Show(false,false);
		pAccountId->Show(false,false);
	}
}
